#import "dfhgwhruebvhrheyluggagerecorder.h"
#import "dfhgwhruebvhrheyfreightencourage.h"
#import "dfhgwhruebvhrheygluemisunderstand.h"
#import "dfhgwhruebvhrheyterritoryexaggerateViewController.h"
@interface dfhgwhruebvhrheyterritoryexaggerateViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UIView *footer;
@property (nonatomic, strong) UITableView *tabIV;
@property (nonatomic, assign) dhgfywegwygeydwregardlessfolk onLineStyle;
@property (nonatomic, strong) NSMutableArray<PPP_AccountItemModel *> *dataArray;
@end
static NSString *cellIdentifier = @"account_list_cell";
@implementation dfhgwhruebvhrheyterritoryexaggerateViewController {
	void(^_result)(BOOL ret);
}
- (instancetype)initWithStyle:(dhgfywegwygeydwregardlessfolk)style {
	if([super init]) {
		_onLineStyle = style;
	}
	return self;
}
- (UIView *)footer {
	if (!_footer) {
		_footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, dhgfywegwygeydwdispositionvisualize, 79)];
		UIButton *addBtn = [[UIButton alloc] initWithFrame:CGRectMake(13, 16, dhgfywegwygeydwdispositionvisualize-26, 63)];
		[addBtn setImage:[UIImage imageNamed:@"home_account_add"] forState:UIControlStateNormal];
		addBtn.backgroundColor = [primary_color dhgfywegwygeydwlearneddesignate:0.1];
		addBtn.layer.borderColor = primary_color.dhgfywegwygeydwlearneddesignate.CGColor;
		addBtn.layer.borderWidth = 1;
		if (self.onLineStyle == dhgfywegwygeydwsemesterfurious) {
			[addBtn setTitle:@"Add Bank Card" forState:UIControlStateNormal];
		}else {
			[addBtn setTitle:@"Add E_Wallet" forState:UIControlStateNormal];
		}
		addBtn.layer.cornerRadius = 10;
		[addBtn setTitleColor:primary_color.dhgfywegwygeydwlearneddesignate forState:UIControlStateNormal];
		[addBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -10, 0, 0)];
		[addBtn addTarget:self action:@selector(addAccountAction) forControlEvents:UIControlEventTouchUpInside];
		[_footer addSubview:addBtn];
	}
	return _footer;
}
- (UITableView *)tabIV {
	if (!_tabIV) {
		_tabIV = [[UITableView alloc] init];
	}
	return _tabIV;
}
- (void)viewDidLoad {
	[super viewDidLoad];
	[self loadUI];
	[self loadData];
}
- (void)addAccountAction {
	__weak typeof(self) weakSelf = self;
	dfhgwhruebvhrheyconcealwarrantViewController *add = (dfhgwhruebvhrheyconcealwarrantViewController *)[UIStoryboard storyboard:@"Home" withidentifier:@"add_account"];
	add.onLineStyle = self.onLineStyle;
	[self.navigationController pushViewController:add animated:YES];
	[add addAccountResult:^(BOOL ret) {
		__strong typeof(weakSelf) strongSelf = weakSelf;
		[strongSelf loadData];
	}];
}
- (void)loadData {
	if (self.onLineStyle == dhgfywegwygeydwsemesterfurious) {
		[self getUserBankList];
	}else {
		[self getUsewEwalletList];
	}
}
- (void)getUserBankList {
	[dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwlambbreast path:bankListApi isEncrypt:YES withParams:nil success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
		__strong typeof(weakSelf) strongSelf = weakSelf;
		dfhgwhruebvhrheyfreightencourage *model = [dfhgwhruebvhrheyfreightencourage yy_modelWithJSON:json];
		if (model.code == 200) {
			[strongSelf.dataArray removeAllObjects];
			[strongSelf.dataArray addObjectsFromArray:model.data];
		}else {
			[dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
		}
		[strongSelf.tabIV reloadData];
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
	}];
}
- (void)getUsewEwalletList {
	[dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwlambbreast path:ewalletListApi isEncrypt:YES withParams:nil success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
		__strong typeof(weakSelf) strongSelf = weakSelf;
		dfhgwhruebvhrheyfreightencourage *model = [dfhgwhruebvhrheyfreightencourage yy_modelWithJSON:json];
		if (model.code == 200) {
			[strongSelf.dataArray removeAllObjects];
			[strongSelf.dataArray addObjectsFromArray:model.data];
		}else {
			[dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
		}
		[strongSelf.tabIV reloadData];
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
	}];
}
- (void)loadUI {
	[self configBackIcon:@"dhgfywegwygeydwcurvereciprocal"];
	self.navigationStyle = dhgfywegwygeydwreciprocaltransport;
	self.tabIV.delegate = self;
	self.tabIV.dataSource = self;
	[self.view addSubview:self.tabIV];
	self.tabIV.tableFooterView = self.footer;
	self.tabIV.rowHeight = UITableViewAutomaticDimension;
	self.navigationItem.title = self.onLineStyle == dhgfywegwygeydwsemesterfurious ? @"Bank Card" : @"E-Wallet";
	self.dataArray = [@[] mutableCopy];
	self.tabIV.separatorStyle = UITableViewCellSeparatorStyleNone;
	[self.tabIV registerNib:[UINib nibWithNibName:@"dfhgwhruebvhrheygluemisunderstand" bundle:nil] forCellReuseIdentifier:cellIdentifier];
}
- (void)bindDefaultAccount:(void(^)(BOOL ret))result {
	_result = result;
}
- (void)selectDefaultAccountResult {
	switch (self.onLineStyle) {
		case dhgfywegwygeydwsemesterfurious: {
			[self getUserBankList];
		}
			break;
		case dhgfywegwygeydwexaggeratesemester: {
			[self getUsewEwalletList];
		}
			break;
		default:
			break;
	}
	if(_result) {
		_result(YES);
	}
}
- (void)toggleDefaultAccount:(PPP_AccountItemModel *)model {
	NSString *apiPath;
	NSMutableDictionary *params = [@{} mutableCopy];
	if (self.onLineStyle == dhgfywegwygeydwsemesterfurious) {
		apiPath = defaultBankApi;
		params[@"bank_id"] = model.b_id;
	}else {
		apiPath = defaultEwalletApi;
		params[@"ew_id"] = model.account_id;
	}
	[dfhgwhruebvhrheyoptionsaving.keyWindow startLoading];
	__weak typeof(self) weakSelf = self;
	[[dfhgwhruebvhrheyluggagerecorder shareInstance] dhgfywegwygeydwbarrierawait:dhgfywegwygeydwawaitlamb path:apiPath isEncrypt:YES withParams:params success:^(NSURLSessionDataTask * _Nonnull task, NSString * _Nonnull json) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
		__strong typeof(weakSelf) strongSelf = weakSelf;
		dfhgwhruebvhrheysternideology *model = [dfhgwhruebvhrheysternideology yy_modelWithJSON:json];
		if (model.code == 200) {
			[strongSelf selectDefaultAccountResult];
			[strongSelf.navigationController popViewControllerAnimated:YES];
		}else {
			[dfhgwhruebvhrheyoptionsaving.keyWindow show:model.error];
		}
	} failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
		[dfhgwhruebvhrheyoptionsaving.keyWindow stopLoading];
	}];
}
- (void)viewDidLayoutSubviews {
	[super viewDidLayoutSubviews];
	[self.tabIV mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.bottom.right.equalTo(self.view);
		make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
	}];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[self toggleDefaultAccount:self.dataArray[indexPath.row]];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	int type = self.onLineStyle == dhgfywegwygeydwexaggeratesemester ? 1 : 2;
	dfhgwhruebvhrheygluemisunderstand *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	[cell bindData:self.dataArray[indexPath.row] byType:type];
	return cell;
}
@end
